/**
 * Project  : monkey
 * Author   : Wu Tian Qiang
 * Date     : 2016/10/24 17:10:51
 */
Ext.define('Monkey.view.permission.PermissionController', {
    extend: 'Monkey.view.BaseController',
    alias: 'controller.ctrl-permission',

    init: function () {

    },

    reloadPermission: function () {
        var store = Ext.getCmp('permissionGrid').getStore();
        store.loadPage(1);
        this.getReferences().name.setValue('');
        this.getReferences().permission.setValue('');
    },

    //显示新增窗口
    addPermission: function () {
        var window = Ext.create('Monkey.view.permission.PermissionWindow');
        window.setTitle('新增权限');
        window.setIcon('resources/icons/16/lightning_add.png');
        window.getViewModel().data.isAdd = true;
        window.show();
    },

    //显示修改窗口
    modifyPermission: function (record) {
        if (Ext.isEmpty(record.data)) {
            var permissionGrid = this.getReferences().permissionGrid;
            var records = permissionGrid.getSelectionModel().getSelection();
            if (records.length == 0) {
                Ext.Msg.show({
                    title: '提示',
                    message: '请先选中需要修改的权限',
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR
                });
                return;
            }
            record = records[0];
        }

        var window = Ext.create('Monkey.view.permission.PermissionWindow');
        window.setTitle('修改权限');
        window.setIcon('resources/icons/16/lightning.png');

        var viewModel = window.getViewModel();
        viewModel.data.isAdd = false;
        viewModel.setModelData(record.data);

        window.show();
    },

    removePermission: function () {
        var me = this;
        var permissionGrid = this.getReferences().permissionGrid;
        var records = permissionGrid.getSelectionModel().getSelection();
        if (records.length == 0) {
            Ext.Msg.show({
                title: '提示',
                message: '请先选中需要删除权限',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }
        Ext.Msg.show({
            title: '请确认',
            message: '确认要删除已选中的权限吗？',
            buttons: Ext.Msg.OKCANCEL,
            fn: function (buttonId) {
                if (buttonId == 'ok') {
                    var ids = Monkey.Utils.arrayModelToString(records, 'id', ',');
                    me.request(Monkey.URL.SERVER_URL + '/m/permission/' + ids + '.json', 'DELETE', null, function (data) {
                        var icon = Ext.Msg.ERROR;
                        if (data.success) {
                            icon = Ext.Msg.INFO;
                            me.reloadPermission();
                        }
                        Ext.Msg.show({
                            title: '提示',
                            message: data.message,
                            buttons: Ext.Msg.OK,
                            icon: icon
                        });
                    });
                }
            },
            icon: Ext.Msg.INFO
        });
    },

    rowdblclick: function (node, record) {
        this.modifyPermission(record);
    },

    ok: function () {
        var view = this.getView(),
            viewModel = this.getViewModel(),
            method = 'PUT',
            me = this;
        var form = view.getReferences().form;
        if (!form.isValid()) {
            return;
        }
        if (viewModel.data.isAdd) {
            method = 'POST';
        }
        this.request(Monkey.URL.SERVER_URL + '/m/permission.json', method, viewModel.data, function (data) {
            Ext.Msg.show({
                title: '提示',
                message: data.message,
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.INFO
            });
            if (data.success) {
                view.close();
                me.reloadPermission();
            }
        });

    },

    cancel: function () {
        this.getView().close();
    },

    //查询按钮相应事件
    findPermission: function () {
        var references = this.getReferences();
        var name = references.name.getValue();
        var permission = references.permission.getValue();
        var store = Ext.getCmp('permissionGrid').getStore();
        store.loadPage(1, {
            params: {'search.name_like': name, 'search.permission_like': permission}
        });
    }

});
